<?php

include('InterfaceKamate.php');

class Date implements InterfaceKamate {

    public $Indate;
    public $Outdate;
    public $host = "localhost";
    public $user = "root";
    public $pass = "toor";
    public $database_name = "vestaci";
    public $base ;
    public $iznos;
    public $total;
    public $payment=array(array());
    public $master=array(array());
    
     

   
    

    
    
    function __construct() {
        $this->connect();
        $this->inicialiseInDate();
        $this->inicialiseOutDate();
        $this->inicialiseBase();
    }
    
   

    function inicialiseInDate() {
        if (isset($_POST['from'])) {
            $this->Indate = $_POST['from'];
        } else {
            echo'No correct IN date';
            exit;
        }
    }

    function inicialiseOutDate() {

        if (isset($_POST['to'])) {
            $this->Outdate = $_POST['to'];
        } else {
            echo'No correct OUT date';
            exit;
        }
    }

    function inicialiseBase() {

        if (isset($_POST['base'])) {
            $this->base = $_POST['base'];
        } else {
            echo'No correct base ammount';
            exit;
        }
    }
    
    function inicialisePayment1(){
        if(isset($_SESSION['payment1'])){
            $uplata1=$_SESSION['iznos_uplate1'];
            $_SESSION['payment1']=$_SESSION['payment1'] . '/' . $uplata1;
            $pay = explode("/", $_SESSION['payment1']);
        if ($this->startsWith($pay[0], '0') == 1) {
            $pay[0] = str_replace('0', '', $pay[0]);
            $pay[0] = (int) $pay[0];          
        }
        if ($this->startsWith($pay[1], '0') == 1) {
            $pay[1] = str_replace('0', '', $pay[1]);
            $pay[1] = (int) $pay[1];          
        }
         if ($this->startsWith($pay[2], '0') == 1) {
            $pay[2] = str_replace('0', '', $pay[2]);
            $pay[2] = (int) $pay[2];          
        }
        $query=mysql_query("SELECT * from kamata where mesec=" . $pay[0] . " and godina=" . $pay[2]  . "");
        $result=mysql_fetch_array($query);
        if($pay[1] == 5 && $pay[2] == 2011){
             if($pay[0]<=16){
              $this->payment[0]['id']=$result['id'];
             }else{
                 $this->payment[0]['id']=$result['id'] +1;
             }
        }elseif($pay[1] == 3 && $pay[2] == 2001){
             if($pay[0]<=2){
              $this->payment[0]['id']=$result['id'];
             }else{
                 $this->payment[0]['id']=$result['id'] +1;
             }
        
        }else{
            $this->payment[0]['id']=$result['id'];
        }
        
        $this->payment[0]['day']=$pay[1];
        $this->payment[0]['month']=$pay[0];
        $this->payment[0]['year']=$pay[2];
        $this->payment[0]['ammount']=$pay[3];
        
        
        }
        
        
    }
    
    function inicialisePayment2(){
        if(isset($_SESSION['payment1'])){
            $uplata1=$_SESSION['iznos_uplate2'];
            $_SESSION['payment1']=$_SESSION['payment1'] . '/' . $uplata1;
            $pay = explode("/", $_SESSION['payment1']);
        if ($this->startsWith($pay[0], '0') == 1) {
            $pay[0] = str_replace('0', '', $pay[0]);
            $pay[0] = (int) $pay[0];          
        }
        if ($this->startsWith($pay[1], '0') == 1) {
            $pay[1] = str_replace('0', '', $pay[1]);
            $pay[1] = (int) $pay[1];          
        }
         if ($this->startsWith($pay[2], '0') == 1) {
            $pay[2] = str_replace('0', '', $pay[2]);
            $pay[2] = (int) $pay[2];          
        }
        $query=mysql_query("SELECT * from kamata where mesec=" . $pay[0] . " and godina=" . $pay[2]  . "");
        $result=mysql_fetch_array($query);
        if($pay[1] == 5 && $pay[2] == 2011){
             if($pay[0]<=16){
              $this->payment[1]['id']=$result['id'];
             }else{
                 $this->payment[1]['id']=$result['id'] +1;
             }
        }elseif($pay[1] == 3 && $pay[2] == 2001){
             if($pay[0]<=2){
              $this->payment[1]['id']=$result['id'];
             }else{
                 $this->payment[1]['id']=$result['id'] +1;
             }
        
        }else{
            $this->payment[1]['id']=$result['id'];
        }
        
        $this->payment[1]['day']=$pay[1];
        $this->payment[1]['month']=$pay[0];
        $this->payment[1]['year']=$pay[2];
        $this->payment[1]['ammount']=$pay[3];
        
        
        }
        
        
    }
    
        
    


    function connect() {
        if (strlen($this->host) || strlen($this->user) || strlen($this->pass) > 2) {
            $con = mysql_connect($this->host, $this->user, $this->pass);
            if ($con) {
                mysql_select_db($this->database_name);
                mysql_set_charset("UTF-8");
                return true;
            } else {
                return false;
            }
        }
    }

    function startsWith($haystack, $needle) {
        return (strpos($haystack, $needle) === 0);
    }

    //vraca datum pocetka kao array
    public function getInDay() {
        $Indate = explode("/", $this->Indate);
        if ($this->startsWith($Indate[1], '0') == 1) {
            $Indate[1] = str_replace('0', '', $Indate[1]);
            $Indate[1] = (int) $Indate[1];
        }
        return $Indate[1];
    }
    
    public function paymentInDay() {
        $Indate = explode("/", $this->Indate);
        if ($this->startsWith($Indate[1], '0') == 1) {
            $Indate[1] = str_replace('0', '', $Indate[1]);
            $Indate[1] = (int) $Indate[1];
        }
        return $Indate[1];
    }

    function getInMonth() {
        $Indate = explode("/", $this->Indate);
        if ($this->startsWith($Indate[0], '0') == 1) {
            $Indate[0] = str_replace('0', '', $Indate[0]);
            $Indate[0] = (int) $Indate[0];
        }
        return $Indate[0];
    }

    function getInYear() {
        $Indate = explode("/", $this->Indate);
        if ($this->startsWith($Indate[2], '0') == 1) {
            $Indate[2] = str_replace('0', '', $Indate[2]);
            $Indate[2] = (int) $Indate[2];
        }
        return $Indate[2];
    }

    public function getOutDay() {
        $Outdate = explode("/", $this->Outdate);
        if ($this->startsWith($Outdate[1], '0') == 1) {
            $Outdate[1] = str_replace('0', '', $Outdate[1]);
            $Outdate[1] = (int) $Outdate[1];
        }
        return $Outdate[1];
    }

    function getOutMonth() {
        $Outdate = explode("/", $this->Outdate);
        if ($this->startsWith($Outdate[0], '0') == 1) {
            $Outdate[0] = str_replace('0', '', $Outdate[0]);
            $Outdate[0] = (int) $Outdate[0];
        }
        return $Outdate[0];
    }

    function getOutYear() {

        $Outdate = explode("/", $this->Outdate);
        if ($this->startsWith($Outdate[2], '0') == 1) {
            $Outdate[2] = str_replace('0', '', $Outdate[2]);
            $Outdate[2] = (int) $Outdate[2];
        }
        return $Outdate[2];
    }

    function getFirstMonth() {
        if ($this->connect() != false) {
            if ($this->getInMonth() == 5 && $this->getInYear() == 2011) {
                if ($this->getInDay() > 16) {
                    $query = mysql_query("SELECT id from kamata where mesec= " . $this->getInMonth() . " and godina=" . $this->getInYear() . " ");
                    $FirstResult1 = mysql_fetch_array($query);
                    $idOf2Half = $FirstResult1['id'] + 1;
                    $query2 = mysql_query("SELECT * from kamata where id= " . $idOf2Half . "  ");
                    $FirstResult = mysql_fetch_array($query2);
                    return $FirstResult;
                }else{
                     $query = mysql_query("SELECT * from kamata where mesec= " . $this->getInMonth() . " and godina=" . $this->getInYear() . " ");
                     $FirstResult = mysql_fetch_array($query);
                     return $FirstResult;
                }
            } elseif ($this->getInMonth() == 3 && $this->getInYear() == 2001) {
                if ($this->getInDay() > 2) {
                    $query = mysql_query("SELECT id from kamata where mesec= " . $this->getInMonth() . " and godina=" . $this->getInYear() . " ");
                    $FirstResult1 = mysql_fetch_array($query);
                    $idOf2Half = $FirstResult1['id'] + 1;
                    $query2 = mysql_query("SELECT * from kamata where id= " . $idOf2Half . "  ");
                    $FirstResult = mysql_fetch_array($query2);
                    return $FirstResult;
                }else{
                     $query = mysql_query("SELECT * from kamata where mesec= " . $this->getInMonth() . " and godina=" . $this->getInYear() . " ");
                     $FirstResult = mysql_fetch_array($query);
                     return $FirstResult; 
                }
            } else {
                $query = mysql_query("SELECT * from kamata where mesec= " . $this->getInMonth() . " and godina=" . $this->getInYear() . " ");
                $FirstResult = mysql_fetch_array($query);
                return $FirstResult;
            }
        } else {
            echo 'not connected';
        }
    }

    function getMiddle() {
        if ($this->connect() != false) {
            $FirstMonth = $this->getFirstMonth();
            $LastMonth = $this->getLastMonth();
            $lastMonthID = $LastMonth['id'] - 1;
            $FirstMonthID = $FirstMonth['id'] + 1;
            $ResultInMiddle = array();

            for ($i = $FirstMonthID; $i < $lastMonthID; $i++) {
                $query = mysql_query("SELECT * from kamata where id=" . $i . " ");
                while ($MiddleResult = mysql_fetch_array($query)) {
                    $ResultInMiddle[$i] = $MiddleResult;
                }
            }
            return $ResultInMiddle;
        } else {
            echo 'not connected';
        }
    }

    function getLastMonth() {
       if ($this->connect() != false) {
            if ($this->getOutMonth() == 5 && $this->getOutYear() == 2011) {
                if ($this->getOutDay() > 16) {
                    $query = mysql_query("SELECT id from kamata where mesec= " . $this->getOutMonth() . " and godina=" . $this->getOutYear() . " ");
                    $FirstResult1 = mysql_fetch_array($query);
                    $idOf2Half = $FirstResult1['id'] + 1;
                    $query2 = mysql_query("SELECT * from kamata where id= " . $idOf2Half . "  ");
                    $FirstResult = mysql_fetch_array($query2);
                    return $FirstResult;
                }else{
                     $query = mysql_query("SELECT * from kamata where mesec= " . $this->getOutMonth() . " and godina=" . $this->getOutYear() . " ");
                     $FirstResult = mysql_fetch_array($query);
                     return $FirstResult;
                }
            } elseif ($this->getOutMonth() == 3 && $this->getOutYear() == 2001) {
                if ($this->getOutDay() > 2) {
                    $query = mysql_query("SELECT id from kamata where mesec= " . $this->getOutMonth() . " and godina=" . $this->getOutYear() . " ");
                    $FirstResult1 = mysql_fetch_array($query);
                    $idOf2Half = $FirstResult1['id'] + 1;
                    $query2 = mysql_query("SELECT * from kamata where id= " . $idOf2Half . "  ");
                    $FirstResult = mysql_fetch_array($query2);
                    return $FirstResult;
                }else{
                     $query = mysql_query("SELECT * from kamata where mesec= " . $this->getOutMonth() . " and godina=" . $this->getOutYear() . " ");
                     $FirstResult = mysql_fetch_array($query);
                     return $FirstResult; 
                }
            } else {
                $query = mysql_query("SELECT * from kamata where mesec= " . $this->getOutMonth() . " and godina=" . $this->getOutYear() . " ");
                $FirstResult = mysql_fetch_array($query);
                return $FirstResult;
            }
        } else {
            echo 'not connected';
        }
    }

    function FirstMonthNOdays() {

        if ($this->getInMonth() == 5 && $this->getInYear() == 2011) {
            if ($this->payment[0]['id'] == 199) {
                return $this->paymentIn1May();
            } elseif ($this->payment[0]['id'] == 200) {
                return $this->paymentIn2May();
            } else {
                return $this->startMay();
            }
        } elseif ($this->getInMonth() == 3 && $this->getInYear() == 2001) {
            if ($this->payment[0]['id'] == 76) {
                return $this->paymentIn1March();
            } elseif ($this->payment[0]['id'] == 77) {
                return $this->paymentIn2March();
            } else {
                return $this->startMarch();
            }
        } else {
            return $this->StartDays();
        }
    }

    function startMay() {
        if ($this->getInDay() <= 16) {
            return $this->startMay1half();
        } else {
            return $this->startMay2half();
        }
    }

    function startMay1half() {
        $startDay = $this->getInDay();
        
        $startMay = 16 - $startDay + 1;
        return $startMay;
        
        
    }

    function startMay2half() {
        $startDay = $this->getInDay();
        
        $startMay = 31 - $startDay + 1;
        return $startMay;
        
      
    }
    
     function startMarch1half() {
        $startDay = $this->getInDay();
        
        $startMarch = 2 - $startDay + 1;
        return $startMarch;
         
    }

    function startMarch2half() {
        $startDay = $this->getInDay();
       
        $startMarch = 31 - $startDay + 1;
        return $startMarch;
         
    }

    
    
     function startMarch() {
        if ($this->getInDay() <= 2) {
            return $this->startMarch1half();
        } else {
            return $this->startMarch2half();
        }
    }

    function StartDays() {
        $startDay = $this->getInDay();
        $noOfDays = $this->getFirstMonth();
        $noOfDays = $noOfDays['broj_dana'];
        $startDays = $noOfDays - $startDay;
        return $startDays;
    }
    
    function endMarch2half() {
        $endMarch = $this->getOutDay();
        $endMarch=$endMarch-2;
        return $endMarch;
    }

     function endMarch1half() {
        $endMarch = $this->getOutDay();
        return $endMarch;
    }

     function endMay2half() {
        $endMay = $this->getOutDay();
        $endMay=$endMay-16;
        return $endMay;
    }

     function endMay1half() {
        $endMay = $this->getOutDay();
        return $endMay;
    }
    
    function paymentIn1May(){
        $startDay = $this->getInDay();
        $startMay1 = $this->payment[0]['day'] - $startDay + 1;
        $startMay2= 16-$startMay1;
        
        return array('day1'=>$startMay1,'day2'=>$startMay2);
    }
    
    function paymentIn2May() {
        $startDay = $this->getInDay();
        $startMay1 = $this->payment[0]['day'] - $startDay + 1;
        $startMay2= 31-$startMay1;
        return array('day1'=>$startMay1,'day2'=>$startMay2);
        
    }


     function paymentIn1March() {
        
    }

     function paymentIn2March() {
        
    }

    //racuna prvi mesec
    function calculateFirst(){
       $days= $this->FirstMonthNOdays();
       echo $days['day1'];
       if(count($days) == 2){
           $days=$days['day1'];
       }
       $tax=$this->getFirstMonth();
       $tax=$tax['mesecna_kamata'];
       $number1= 1 + $tax / 100; 
       $number2=$this->FirstMonthNOdays() / 31;
       $stopa=pow($number1,$number2) -1 ;
       $iznos = $this->base * $stopa;
       $iznos = round($iznos, 2);
       $total = $iznos + $this->base;
       $total = round($total, 2);
       $total=number_format($total, 2, '.', '');	
       $this->iznos=$iznos;
       $this->total=$total;
       
       return array('stopa'=>$tax,'total'=>$total,'broj_dana'=>$days,'iznos_sa_kamatom'=>$iznos);
        
        
    }
    //vraca header tabele i iznos do kraja prvog meseca ili do prve uplate
    function  renderFirstTable(){
         $firstTable=$this->calculateFirst();
         echo '<table  class="table1"><tr><th>Месец</th><th>број дана</th><th>година</th><th>стопа %</th>
                <th>основица</th><th>камата</th><th>укупно</th></tr>';
         echo '<tr class="row"><td>' . $this->getInMonth() . '</td><td>' . $firstTable['broj_dana'] . '</td><td>' . $this->getInYear() . '</td><td>'
               . $firstTable['stopa'] .  '</td><td>' . $this->base . '</td><td>' . $firstTable['iznos_sa_kamatom'] . '</td><td>' . $firstTable['total'] . '</td></tr>';	
        //return array('header'=>$header,'table1'=>$StTable);
        
    }
    
    function masterFunc(){
        $a=$this->getFirstMonth();
        $a['BROJ_DANA']=$a['broj_dana'];
        if($a['id']==199){
            $a['broj_dana']=$this->startMay1half();
        }elseif($a['id']==200){
            $a['broj_dana']=$this->startMay2half();
        }elseif($a['id'] == 76){
            $a['broj_dana']=$this->startMarch1half();
        }elseif($a['id'] == 77){
            $a['broj_dana']=$this->startMarch2half();
        }else{
           
            $a['broj_dana']=$a['broj_dana'] - $this->getInDay() +1;
            
        }
       
        
        $this->master[0]=$a;
        
         if ($this->connect() != false) {
            $FirstMonth = $this->getFirstMonth();
            $LastMonth = $this->getLastMonth();
            $lastMonthID = $LastMonth['id'] ;
            $FirstMonthID = $FirstMonth['id']+1 ;
            

            for ($i = $FirstMonthID; $i < $lastMonthID; $i++) {
                $query = mysql_query("SELECT * from kamata where id=" . $i . " ");
                while ($MiddleResult = mysql_fetch_array($query)) {
                    $this->master[$i] = $MiddleResult;
                }
            }
            
        } else {
            echo 'not connected';
        }
        
        $b=$this->getLastMonth();
        $b['BROJ_DANA']=$b['broj_dana'];
         if($b['id']==199){
            $b['broj_dana']=$this->endMay1half();
        }elseif($b['id']==200){
            $b['broj_dana']=$this->endMay2half();
        }elseif($b['id'] == 76){
            $b['broj_dana']=$this->endMarch1half();
        }elseif($b['id'] == 77){
            $b['broj_dana']=$this->endMarch2half();
        }else{
            $b['broj_dana']=$this->getOutDay();
        }
        $x=array_pop(array_keys($this->master));
        $x=$x+1;
        
        $this->master[$x]=$b;
        return $this->master;
        
        
    }
    
    

}

?>
